﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EasyPayDemo.WeiPay
{
    public class WeiPayUtils
    {
        private static Random rd = new Random();
        public static string GetOrgTrace(string OrgId)
        {
            //Random rd = new Random();
            return OrgId.Substring(0, 4) + OrgId.Substring(OrgId.Length - 4) + DateTime.Now.ToString("yyyyMMddHHmmss") + ((ulong)((rd.NextDouble() * 9 + 1) * 100000)).ToString();
        }

        public static string GetAesKey()
        {
            return "ASDEFC" + ((ulong)((rd.NextDouble() * 9 + 1) * 1000000000)).ToString() + "frtghy" + ((ulong)((rd.NextDouble() * 9 + 1) * 1000000000)).ToString();

        }

        public static string GetSignData<T>(T data)
        {
            var propertis = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Select(p => p.Name).ToArray();

            ArrayList keys = new ArrayList(propertis);
            keys.Sort(StringComparer.Ordinal);
            StringBuilder chkStr = new StringBuilder();



            foreach (string str in keys)
            {
                var value = data.GetType().GetProperty(str).GetValue(data, null);
                if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
                {
                    chkStr.AppendFormat("{0}={1}&", str, value);
                }

            }
            chkStr.Remove(chkStr.Length - 1, 1);

            Console.WriteLine(chkStr.ToString());
            return chkStr.ToString();
        }
    }
}
